package com.foreknow.view;

import com.foreknow.model.Business;
import com.foreknow.service.impl.BusinessServiceImpl;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;

public class Update extends JFrame {

	private JPanel contentPane;
	private JTextField bName;
	private JTextField bPassword;
	private JTextField bAddress;
	private JTextField bExplain;
	private JTextField starPrice;
	private JTextField deliveryPrice;
	private int bid;

	public int getBid() {
		return bid;
	}

	public void setBid(int bid) {
		this.bid = bid;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Update frame = new Update();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Update() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 710, 389);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label5 = new JLabel("商家名称：");
		label5.setFont(new Font("楷体", Font.PLAIN, 20));
		label5.setHorizontalAlignment(SwingConstants.RIGHT);
		label5.setBounds(40, 40, 126, 24);
		contentPane.add(label5);
		
		JLabel label = new JLabel("密码：");
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		label.setFont(new Font("楷体", Font.PLAIN, 20));
		label.setBounds(40, 77, 126, 24);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("商家地址：");
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		label_1.setFont(new Font("楷体", Font.PLAIN, 20));
		label_1.setBounds(40, 114, 126, 24);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("商家介绍：");
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);
		label_2.setFont(new Font("楷体", Font.PLAIN, 20));
		label_2.setBounds(40, 151, 126, 24);
		contentPane.add(label_2);
		
		JLabel label_3 = new JLabel("起送费：");
		label_3.setHorizontalAlignment(SwingConstants.RIGHT);
		label_3.setFont(new Font("楷体", Font.PLAIN, 20));
		label_3.setBounds(40, 188, 126, 24);
		contentPane.add(label_3);
		
		JLabel label_4 = new JLabel("配送费：");
		label_4.setHorizontalAlignment(SwingConstants.RIGHT);
		label_4.setFont(new Font("楷体", Font.PLAIN, 20));
		label_4.setBounds(40, 225, 126, 24);
		contentPane.add(label_4);
		
		bName = new JTextField();
		bName.setBounds(180, 42, 381, 24);
		contentPane.add(bName);
		bName.setColumns(10);
		
		bPassword = new JTextField();
		bPassword.setColumns(10);
		bPassword.setBounds(180, 79, 381, 24);
		contentPane.add(bPassword);
		
		bAddress = new JTextField();
		bAddress.setColumns(10);
		bAddress.setBounds(180, 116, 381, 24);
		contentPane.add(bAddress);
		
		bExplain = new JTextField();
		bExplain.setColumns(10);
		bExplain.setBounds(180, 153, 381, 24);
		contentPane.add(bExplain);
		
		starPrice = new JTextField();
		starPrice.setColumns(10);
		starPrice.setBounds(180, 190, 381, 24);
		contentPane.add(starPrice);
		
		deliveryPrice = new JTextField();
		deliveryPrice.setColumns(10);
		deliveryPrice.setBounds(180, 227, 381, 24);
		contentPane.add(deliveryPrice);
		
		JButton yes = new JButton("确定");
		yes.setFont(new Font("楷体", Font.PLAIN, 20));
		yes.setBounds(125, 285, 104, 33);
		contentPane.add(yes);
		yes.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				BusinessServiceImpl bService = new BusinessServiceImpl();
				Business business = new Business();
				business.setPassword(bPassword.getText());
				business.setBusinessName(bName.getText());
				business.setBusinessAddress(bAddress.getText());
				business.setBusinessExplain(bExplain.getText());
				business.setStarPrice(BigDecimal.valueOf(Float.valueOf(starPrice.getText())));
				business.setDeliveryPrice(BigDecimal.valueOf(Float.valueOf(deliveryPrice.getText())));
				bService.update(business, getBid());
				System.out.println(getBid());

				MainWindow mainWindow = new MainWindow();
				mainWindow.refresh();
				Update.this.dispose();
			}
		});
		
		JButton no = new JButton("取消");
		no.setFont(new Font("楷体", Font.PLAIN, 20));
		no.setBounds(437, 285, 104, 33);
		contentPane.add(no);
		no.addActionListener(new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent actionEvent) {
				MainWindow mainWindow = new MainWindow();
				Update.this.dispose();
			}
		});
		this.setVisible(true);
	}
}
